package com.ruoyi.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.vo.TreeSelect;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.SysDept;
import com.ruoyi.mapper.SysDeptMapper;

@Service
public class SysDeptService {
	
	@Autowired
	private SysDeptMapper sysDeptMapper;
	
	/**
	 * 查找部门列表
	 * @param dept
	 * @return
	 */
	public List<SysDept> selectDeptList(SysDept dept) {
		return sysDeptMapper.selectDeptList(dept);
	}
	
	public List<TreeSelect> selectDeptTreeList(SysDept dept) {
		List<SysDept> depts = sysDeptMapper.selectDeptList(dept);
		return buildDeptTreeSelect(depts);
	}
	
	public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) {
		List<SysDept> deptTrees = buildDeptTree(depts);
		return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
	}
	
	public List<SysDept> buildDeptTree(List<SysDept> depts) {
		List<SysDept> returnList = new ArrayList<SysDept>();
		List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
		for (SysDept dept : depts) {
			if (!tempList.contains(dept.getParentId())) {
				recursionFn(depts,dept);
				returnList.add(dept);
			}
		}
		if (returnList.isEmpty()) {
			returnList = depts;
		}
		return returnList;
	}

	private void recursionFn(List<SysDept> list, SysDept dept) {
		List<SysDept> childList = getChildList(list,dept);
		dept.setChildren(childList);
		for (SysDept tChild : childList) {
			if (hasChild(list,tChild)) {
				recursionFn(list, tChild);
			}
		}
	}



	private List<SysDept> getChildList(List<SysDept> list, SysDept dept) {
		List<SysDept> tlist = new ArrayList<SysDept>();
		Iterator<SysDept> it = list.iterator();
		while (it.hasNext()) {
			SysDept n = (SysDept) it.next();
			if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == dept.getDeptId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}
	
	private boolean hasChild(List<SysDept> list, SysDept tChild) {
		return getChildList(list, tChild).size() > 0;
	}

	public int insertDept(SysDept dept) {
		SysDept info = sysDeptMapper.selectDeptById(dept.getParentId());
		if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
			throw new ServiceException("部门停用，不允许新增");
		}
		dept.setAncestors(info.getAncestors() + "," +dept.getParentId());
		return sysDeptMapper.insertDept(dept);

		
	}

	public boolean checkDeptNameUnique(SysDept dept) {
		Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
		SysDept info = sysDeptMapper.checkDeptNameUnique(dept.getDeptName(),dept.getParentId());
		if (StringUtils.isNotNull(info)  && info.getDeptId().longValue() != deptId.longValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	public int selectNormalChildrenDeptById(Long deptId) {
		return sysDeptMapper.selectNormalChildrenDeptById(deptId);
	}

	public int updateDept(SysDept dept) {
		SysDept newParentDept = sysDeptMapper.selectDeptById(dept.getParentId());
		SysDept oldDept = sysDeptMapper.selectDeptById(dept.getDeptId());
		if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
			String newAncestors = newParentDept.getAncestors() + "," +newParentDept.getParentId();
			String oldAncestors = oldDept.getAncestors();
			dept.setAncestors(newAncestors);
			updateDeptChildren(dept.getDeptId(),newAncestors,oldAncestors);
		}
		int result = sysDeptMapper.updateDept(dept);
		if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) && !StringUtils.equals("0", dept.getAncestors())) {
			updateParentDeptStatusNormal(dept);
		}
		return result;
	}

	private void updateParentDeptStatusNormal(SysDept dept) {
		String ancestor = dept.getAncestors();
		Long[] deptIds = Convert.toLongArray(ancestor);
		sysDeptMapper.updateDeptStatusNormal(deptIds);
	}

	public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
		List<SysDept> children = sysDeptMapper.selectChildrenDeptById(deptId);
		for (SysDept child :children) {
			child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
		}
		if (children.size() > 0) {
			sysDeptMapper.updateDeptChildren(children);
		}
	}

	public SysDept selectDeptById(Long deptId) {
		return sysDeptMapper.selectDeptById(deptId);
	}

	public boolean hasChildByDeptId(Long deptId) {
		int result = sysDeptMapper.hasChildByDeptId(deptId);
		return result > 0 ;
	}

	public boolean checkDeptExistUser(Long deptId) {
		int result = sysDeptMapper.checkDeptExistUser(deptId);
		return result > 0;
	}

	public int deleteDeptById(Long deptId) {
		return sysDeptMapper.deleteDeptById(deptId);
	}
	
}
